<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Attitude_Criterias_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('xlsimport/php-excel-reader/excel_reader2');
        $this->load->helper('xlsimport/spreadsheetreader.php');
        $this->load->model('attitudecriteria', '', true);
        $this->load->model('user', '', true);

        if (!$this->session->userdata('logged_in')) {
            redirect('login');
        }

        //$this->LoggedUser = $this->user->getLoggedUser();
        
        $this->load->library('authentication');$this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();$this->authentication->redirectIfNotBe( array('HR', 'SC'));
    }

    function index($limit = '0') {
//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }

        $this->load->helper('form');
        $this->load->model('evaluation', '', true);
        $evl = $this->evaluation->get_last();

//        $viewData['category'] = $this->category->category_dropdown();
//        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();
//        $viewData['result'] = $this->qbank_model->question_list($limit);
//        $viewData['title'] = "Question Bank";
//        $viewData['limit'] = $limit;
        
        $criterias = $this->attitudecriteria->getAll();
        
        // thống kê
        foreach ($criterias as &$eachCriteria) {
            $result = $this->attitudecriteria->getLevelsRatio($eachCriteria['id'], $evl['id']);
            $eachCriteria['highLevel'] = $result['High'];
            $eachCriteria['mediumLevel'] = $result['Medium'];
            $eachCriteria['lowLevel'] = $result['Low'];
            
            $eachCriteria['highLevelRatio'] = $eachCriteria['highLevel']*100/($eachCriteria['highLevel']+$eachCriteria['mediumLevel']+$eachCriteria['lowLevel']);
            $eachCriteria['mediumLevelRatio'] = $eachCriteria['mediumLevel']*100/($eachCriteria['highLevel']+$eachCriteria['mediumLevel']+$eachCriteria['lowLevel']);
            $eachCriteria['lowLevelRatio'] = $eachCriteria['lowLevel']*100/($eachCriteria['highLevel']+$eachCriteria['mediumLevel']+$eachCriteria['lowLevel']);
        }
        
        $viewData['attitudeCriterias'] = $criterias;

        $this->load->view(get_folder($this->loggedUser->title).'/attitude_criterias_list', $viewData);
    }

    function import() {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }
        if (isset($_FILES['xlsfile'])) {
            $targets = 'xls/';
            $targets = $targets . basename($_FILES['xlsfile']['name']);
            $docadd = ($_FILES['xlsfile']['name']);
            if (move_uploaded_file($_FILES['xlsfile']['tmp_name'], $targets)) {
                $Filepath = $targets;
                $allxlsdata = array();
                date_default_timezone_set('UTC');

                $StartMem = memory_get_usage();
                //echo '---------------------------------'.PHP_EOL;
                //echo 'Starting memory: '.$StartMem.PHP_EOL;
                //echo '---------------------------------'.PHP_EOL;

                try {
                    $Spreadsheet = new SpreadsheetReader($Filepath);
                    $BaseMem = memory_get_usage();

                    $Sheets = $Spreadsheet->Sheets();

                    //echo '---------------------------------'.PHP_EOL;
                    //echo 'Spreadsheets:'.PHP_EOL;
                    //print_r($Sheets);
                    //echo '---------------------------------'.PHP_EOL;
                    //echo '---------------------------------'.PHP_EOL;

                    foreach ($Sheets as $Index => $Name) {
                        //echo '---------------------------------'.PHP_EOL;
                        //echo '*** Sheet '.$Name.' ***'.PHP_EOL;
                        //echo '---------------------------------'.PHP_EOL;

                        $Time = microtime(true);

                        $Spreadsheet->ChangeSheet($Index);

                        foreach ($Spreadsheet as $Key => $Row) {
                            //echo $Key.': ';
                            if ($Row) {
                                //print_r($Row);
                                $allxlsdata[] = $Row;
                            } else {
                                var_dump($Row);
                            }
                            $CurrentMem = memory_get_usage();

                            //echo 'Memory: '.($CurrentMem - $BaseMem).' current, '.$CurrentMem.' base'.PHP_EOL;
                            //echo '---------------------------------'.PHP_EOL;

                            if ($Key && ($Key % 500 == 0)) {
                                //echo '---------------------------------'.PHP_EOL;
                                //echo 'Time: '.(microtime(true) - $Time);
                                //echo '---------------------------------'.PHP_EOL;
                            }
                        }

                        //	echo PHP_EOL.'---------------------------------'.PHP_EOL;
                        //echo 'Time: '.(microtime(true) - $Time);
                        //echo PHP_EOL;
                        //echo '---------------------------------'.PHP_EOL;
                        //echo '*** End of sheet '.$Name.' ***'.PHP_EOL;
                        //echo '---------------------------------'.PHP_EOL;
                    }
                } catch (Exception $E) {
                    echo $E->getMessage();
                }


                $this->qbank_model->import_question($allxlsdata);
            }
        } else {
            echo "Error: " . $_FILES["file"]["error"];
        }
        redirect('qbank');
    }

    function add() {

        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }
//        $this->load->model('category', '', TRUE);
//        $viewData['category'] = $this->category->category_dropdown();
        
//        $this->load->model('difficult_level', '', TRUE);
//        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();
        
//        $viewData['title'] = "Add new question";

        if ($this->input->post('Command') && $this->input->post('Command') == 'Add') {
            // Thực hiện form validation
            
            $newAttitudeCriteria = array(
                'name' => $this->input->post('name'),
                'score' => $this->input->post('d1').','.$this->input->post('d2').','.$this->input->post('d3')
            );
            
            $viewData['addResult'] = $this->attitudecriteria->insert($newAttitudeCriteria);
        }
        

        $this->load->view(get_folder($this->loggedUser->title).'/attitude_criteria_add', $viewData);
    }

    function edit($id) {

//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }


//        $this->load->model('category', '', TRUE);
//        $viewData['category'] = $this->category->category_dropdown();
//
//        $this->load->model('difficult_level', '', TRUE);
//        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();

        if ($this->input->post('Command') && $this->input->post('Command') == 'Edit') {
            //print_r($_POST);
            
            $newAttitudeCriteria = array(
                'id' => $id,
                'name' => $this->input->post('name'),
                'score' => $this->input->post('d1').','.$this->input->post('d2').','.$this->input->post('d3')
            );
            
            $viewData['editResult'] = $this->attitudecriteria->update($newAttitudeCriteria);
        }
        
        $viewData['attitudeCriteria'] = $this->attitudecriteria->get($id);

//        $viewData['title'] = "Edit question";

        $this->load->view(get_folder($this->loggedUser->title).'/attitude_criteria_edit', $viewData);
    }

    function delete($id) {
//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }
        $viewData['deleteResult'] = $this->attitudecriteria->delete($id);

        redirect(get_folder($this->loggedUser->title).'/attitude_criterias', 'refresh');
    }

// get desired question for particular subject and difficulty level
    function get_level_question($difficulty_level, $category) {
        $this->db->where("cid", $category);
        $this->db->where("did", $difficulty_level);
        $query = $this->db->get("qbank");
        $num = $query->num_rows();
        echo $num;
    }

}
?>













